#ifndef _ITEXTBLOCK_H_
#define _ITEXTBLOCK_H_

#include <BPainter>
#include <BTextFormat>
#include "ICodeCell.h"

namespace BWE
{
	class ITextBlock : public BRect
	{
	public:
		ITextBlock();
		ITextBlock(const ITextBlock& other);
		~ITextBlock();

		inline void clear()
		{
			text.clear();
			reset();
		}
		inline bool empty() const
		{
			return cells.empty();
		}
		inline bool valid() const
		{
			return cells.size();
		}

		void setStyle(const BStyle* style);
		void setFormat(BTextFormat* format);
		BTextFormat* format();

		void flushText();

		void reset();
		void reset(const BString& text);
		void append(const BString& text);
		bool remove(int pos, int count = 1);
		bool insert(const BCode& code);
		bool insert(const BString& text);

		void substr(BString& str, int pos, int count) const;
		void selection(BString& str) const;

		bool selectWord();
		bool removeSelection();

		bool seekHead();
		bool seekPoint(const BPoint& point);
		bool seekTail();

		int seekPrevCode();
		int seekNextCode();
		int seekPrevWord();
		int seekNextWord();

		int indexAt(const BPoint& pos) const;
		int wordWidth(int pos, int& end) const;

		void adjust(int pageWidth);
		void paint(BPainter& painter) const;

		using BRect::operator=;

	public:
		bool					visible;
		BString					text;
		Align					align;
		bool					wordWrap;
		int						lineSize;
		int						lineSpacing;

		bool					dirty;
		int						inputIndex;
		int						inputCount;
		BIntArray				rowHeights;
		BArray<ICodeCell>		cells;
		BHolder<const BFont>	font;

	protected:
		BHolder<const BStyle>	_style;
		BHolder<BTextFormat>	_format;
	};

}

#endif